package com.hutao.controller;

import com.hutao.pojo.Address;
import com.hutao.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

/**
 * @author HUTAO
 * @Description
 * @date 2022/3/9 18:15
 */
@Controller
@RequestMapping("/center")
public class AddressController {
	
	@Autowired
	private AddressService addressService;
	
	/**
	 * 去地址管理页面
	 * @return
	 */
	@RequestMapping("/address")
	public String toAddressPage(@RequestParam("cid") int customerid, Model model){
		List<Address> address = addressService.getAddress(customerid);
		model.addAttribute("addressList",address);
		return "forward:/center-setting-address.jsp";
	}
	
	/**
	 * 去修改地址页面
	 * @param addressid
	 * @param model
	 * @return
	 */
	@RequestMapping("/toUpdateAddressPage")
	public String toAddAddressPage(int addressid, Model model){
		Address address = addressService.getAddressByAid(addressid);
		model.addAttribute("address",address);
		return "forward:/updateaddress.jsp";
	}
	
	/**
	 * 修改地址
	 * @param address
	 * @return
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public String updateAddress(Address address,Model model){
		addressService.updateAddress(address);
		List<Address> addList = addressService.getAddress(address.getCustomerid());
		model.addAttribute("addressList",addList);
		return "forward:/center-setting-address.jsp";
	}
	
	/**
	 * 删除地址信息
	 * @param addressid
	 * @param customerid
	 * @param model
	 * @return
	 */
	@RequestMapping("/delete")
	public String deleteAddress(int addressid,int customerid,Model model){
		addressService.deleteAddress(addressid);
		List<Address> addList = addressService.getAddress(customerid);
		model.addAttribute("addressList",addList);
		return "forward:/center-setting-address.jsp";
	}
	
	/**
	 * 添加地址
	 * @param address
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public String addAddress(Address address,Model model){
		addressService.addAddress(address);
		List<Address> addList = addressService.getAddress(address.getCustomerid());
		model.addAttribute("addressList",addList);
		return "forward:/center-setting-address.jsp";
	}
}
